<template>
  <div class="cart-sku" ref="target">
    <div class="attrs" @click="toggle">
      <span class="ellipsis">{{attrsText}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="layer" v-if="visible">
      <div class="loading"  v-if="loading"></div>
      <GoodsSku v-else @change="changeSku"  :goodsData='goodsData' :skuId="skuId" />
      <XtxButton @click="Confirm" type="primary" size="mini" style="margin-left:60px">确认</XtxButton>

    </div>
  </div>
</template>
<script>
import GoodsSku from '@/views/Goods/cpmponents/goods-sku.vue'
import { getSpecsAndSkus } from '@/api/goods'
import { onClickOutside } from '@vueuse/core'
import { ref } from 'vue'
export default {
  name: 'CartSku',
  components: { GoodsSku },
  props: {
    attrsText: { type: String, default: '' },
    skuId: { type: String, default: '' }
  },

  setup (props, { emit }) {
    // 控制面板显示隐藏
    const visible = ref(false)
    const goodsData = ref(null)
    const loading = ref(true)
    const open = () => {
      // 补充loading
      visible.value = true
      // 面板打开根据skuId获取对应的商品信息
      visible.value = true
      getSpecsAndSkus(props.skuId).then(data => {
        console.log('getSpecsAndSkus', data.result)
        loading.value = false
        goodsData.value = data.result
      })
    }
    const close = () => { visible.value = false }
    const toggle = () => { visible.value ? close() : open() }
    const target = ref(null)
    onClickOutside(target, () => {
      close()
    })
    // 选择sku时候触发
    const currSku = ref(null)
    const changeSku = (sku) => {
      currSku.value = sku
    }
    // 点击确认的时候，提交sku信息给购物车组件
    const Confirm = () => {
      // 通知父组件，用户确认了修改
      // 判断是否选择全部的规格
      if (currSku.value && !currSku.value.skuId && currSku.value.skuId === props.skuId) {
        return
      }
      // 如果没有改动

      console.log('通知父组件用户选择的新的', currSku.value)
      emit('change', currSku.value)
      close()
    }
    return { toggle, visible, target, goodsData, loading, changeSku, Confirm }
  }
}
</script>
<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display:inline-block;
  .attrs {
    line-height: 24px;
    display: flex;
    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }
    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }
  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor,50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;
    &::before {
      content: "";
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(.8,1) rotate(45deg);
    }
    .loading {
      height: 224px;
      background: url('~@/assets/images/loading.gif') no-repeat center;
    }
  }
}
</style>
